package com.nbcio.modules.estar.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nbcio.modules.erp.apithird.entity.SysUser;
import com.nbcio.modules.erp.apithird.service.IErpThirdService;
import com.nbcio.modules.estar.order.entity.ErpOrder;
import com.nbcio.modules.estar.order.entity.ErpOrderB;
import com.nbcio.modules.estar.order.mapper.ErpOrderBMapper;
import com.nbcio.modules.estar.order.mapper.ErpOrderMapper;
import com.nbcio.modules.estar.order.service.IErpOrderService;
import org.jeecg.common.api.vo.Result;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 订单
 * @Author: nbacheng
 * @Date:   2024-03-29
 * @Version: V1.0
 */
@Service
public class ErpOrderServiceImpl extends ServiceImpl<ErpOrderMapper, ErpOrder> implements IErpOrderService {

	@Autowired
	private ErpOrderMapper erpOrderMapper;
	@Autowired
	private ErpOrderBMapper erpOrderBMapper;

	@Resource
	private IErpThirdService iErpThirdService;
	
	@Override
	@Transactional
	public void saveMain(ErpOrder erpOrder, List<ErpOrderB> erpOrderBList) {
		erpOrderMapper.insert(erpOrder);
		if(erpOrderBList!=null && erpOrderBList.size()>0) {
			for(ErpOrderB entity:erpOrderBList) {
				//外键设置
				entity.setOrderId(erpOrder.getId());
				erpOrderBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ErpOrder erpOrder,List<ErpOrderB> erpOrderBList) {
		erpOrderMapper.updateById(erpOrder);
		
		//1.先删除子表数据
		erpOrderBMapper.deleteByMainId(erpOrder.getId());
		
		//2.子表数据重新插入
		if(erpOrderBList!=null && erpOrderBList.size()>0) {
			for(ErpOrderB entity:erpOrderBList) {
				//外键设置
				entity.setOrderId(erpOrder.getId());
				erpOrderBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		erpOrderBMapper.deleteByMainId(id);
		erpOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			erpOrderBMapper.deleteByMainId(id.toString());
			erpOrderMapper.deleteById(id);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result approvePass(String id) throws Exception {
		ErpOrder erpOrder = erpOrderMapper.selectById(id);
		if (erpOrder == null) {
			return Result.error("订单不存在！");
		}
		if (erpOrder.getActstatus() == 2 ) {
			return Result.error("订单已审核通过，不能再次审核！");
		}

		SysUser loginUser = iErpThirdService.getLoginUser();
		LambdaUpdateWrapper<ErpOrder> updateOrderWrapper = Wrappers.lambdaUpdate(ErpOrder.class)
				.set(ErpOrder::getApproveBy, loginUser.getUsername())
				.set(ErpOrder::getApproveTime, LocalDateTime.now())
				.eq(ErpOrder::getId, erpOrder.getId());
		erpOrder.setActstatus(2); //审核通过标志 2代表通过    
		if(erpOrderMapper.update(erpOrder, updateOrderWrapper) != 1) {
			return Result.error("订单信息已过期，请刷新重试！");
		}
		return Result.OK("审核通过完成");
	}

}
